<?php


namespace app\index\controller\xilujob;


use app\common\controller\xilujob\Xilujob;
use app\common\model\xilujob\Benefit;
use app\common\model\xilujob\CompanyProperties;
use app\common\model\xilujob\CompanyScale;
use app\common\model\xilujob\EnterpriseInfo;
use app\common\model\xilujob\IndustryCategory;
use app\common\model\xilujob\SiteCity;
use think\Db;
use think\Exception;

class Enterprise extends Xilujob
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    protected $restrictedActions = [];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = new EnterpriseInfo();
    }

    public function edit(){

        if($this->request->isPost()) {
            $auditInfo = $this->request->post('audit/a');
            $info = $this->request->post('basic/a');

            Db::startTrans();
            try {
                $ret = EnterpriseInfo::submitEnterpriseAuditInfo($this->company, $auditInfo);
                $ret = EnterpriseInfo::saveInfo($this->company->id, $info);
            }catch (Exception $exception){
                Db::rollback();
                $this->error($exception->getMessage());
            }
            Db::commit();

            $this->success();
        }

        $enterpriseinfo = EnterpriseInfo::get(['company_id'=>$this->company->id]);

        $enterpriseinfo->environment_images = $enterpriseinfo->environment_images ?? '' ;
        //halt($enterpriseinfo->toArray());
        $this->assign('enterpriseinfo', $enterpriseinfo);
        $this->assign('cities', SiteCity::getAll());
        $this->assign('industry_categories', IndustryCategory::getAll());
        $this->assign('company_scales', CompanyScale::getAll());
        $this->assign('company_types', CompanyProperties::getAll());
        $this->assign('benefits', Benefit::getBenefits());
        $this->assign('benefit_ids_arr', $enterpriseinfo->benefit_ids ? explode(',',$enterpriseinfo->benefit_ids):[]);

        $this->assign('title', '企业信息');
        return $this->fetch();

    }

}